<?php
Class Controller_categorias Extends Controller_Base
{
	
	private function arvore_to_ul($raiz, $metodo)
	{
		$numfilhos = count($raiz);
    	if(!$numfilhos)
    	{
    		return;
    	}
    	
    	$numfilhos = count($raiz['link_nome']);
    	$my_ul = '';
    	if($raiz['pai'][0] != 0)
    		$my_ul = '<UL>';
		for($i = 0; $i < $numfilhos; $i++)
		{
			$my_ul .= "<li><a href='/suser/index.php?route=categorias/" . $metodo . "/" . $raiz['id'][$i] . "' id='node2'>" . $raiz['link_nome'][$i] . "</a>\n" . $this->arvore_to_ul($raiz[$i], $metodo) . "</li>";
			
		}
		if($raiz['pai'][0] != 0)
		$my_ul .= '</UL>';
		
    	/*
    	foreach($arvore as $no)
    	{
    		array_push($arvore, $this->gen_arvore($no['id']));
    	}
    	*/
    	return $my_ul;
	}
	
	function index() {
		$extra_links = Array();
		array_push($extra_links,"<link rel='stylesheet' href='/suser/public/css/arvore.css' type='text/css' media='screen' charset='utf-8'/>\n");
		
		$this->registry['template']->set('extra_links', $extra_links);
		
		//$raiz = $this->registry['categorias']->gen_arvore(0);
		$this->registry['template']->set('contents' , $this->arvore_to_ul($raiz, "ve"));
		$this->registry['template']->show('common/banner');
		$this->registry['template']->show('categorias/ver');
		$this->registry['template']->show('common/footer');
    }
    
    function renderarvore()
    {
    	$raiz = $this->registry['categorias']->gen_arvore(0);
		$this->registry['template']->set('contents' , $this->arvore_to_ul($raiz, ""));
    	$this->registry['template']->show('categorias/arvore');
    }
    
    function adicionar()
    {
    	$extra_links = Array();
		array_push($extra_links,"<link rel='stylesheet' href='/suser/public/css/arvore.css' type='text/css' media='screen' charset='utf-8'/>\n");
		
		$this->registry['template']->set('extra_links', $extra_links);
		
		$raiz = $this->registry['categorias']->gen_arvore(0);
		$this->registry['template']->set('contents' , $this->arvore_to_ul($raiz, "adiciona"));
		$this->registry['template']->show('common/banner');
		$this->registry['template']->show('categorias/adicionar');
		$this->registry['template']->show('common/footer');
    }
    
	function adiciona()
    {
    	echo framework_dir . "<br />";
    	$my_id = $this->registry['Uri']->getURIsegmento(2);
    	$my_linguas = $this->registry['lingua']->get_linguas_disponiveis();
    	
    	if($_POST)
    	{
    		
    		if($this->registry['categorias']->add($my_id, $_POST))
    		{
    			//header("location:/suser/index.php?route=categorias/adicionar");
				exit;
    		} else {
    			echo "<h1>ocorreu um erro</h1><br />";
    		}
    	} else {
    		$this->registry['template']->set('add_id', $my_id);   		
    		$this->registry['template']->set('idiomas', $my_linguas);
			$this->registry['template']->show('common/banner');
			$this->registry['template']->show('categorias/adiciona');
			$this->registry['template']->show('common/footer');
    	}
    }
    
	function ve()
    {
    	$my_id = $this->registry['Uri']->getURIsegmento(2);
    	$my_linguas = $this->registry['lingua']->get_linguas_disponiveis();
    	$my_categoria = $this->registry['categorias']->get($my_id);
    	
    	if($_POST)
    	{
    		
    		if($this->registry['categorias']->update($my_id, $_POST))
    		{
    			header("location:/suser/index.php?route=categorias/index");
				exit;
    		} else {
    			echo "<h1>ocorreu um erro</h1><br />";
    		}
    	} else {
    		$this->registry['template']->set('ve_id', $my_id);
    		$this->registry['template']->set('data', $my_categoria);
    		
    		$this->registry['template']->set('idiomas', $my_linguas);
			$this->registry['template']->show('common/banner');
			$this->registry['template']->show('categorias/ve');
			$this->registry['template']->show('common/footer');
    	}
    }
    
	function remover()
    {
    	$extra_links = Array();
		array_push($extra_links,"<link rel='stylesheet' href='/suser/public/css/arvore.css' type='text/css' media='screen' charset='utf-8'/>\n");
		
		$this->registry['template']->set('extra_links', $extra_links);
		
		$raiz = $this->registry['categorias']->gen_arvore(0);
		$this->registry['template']->set('contents' , $this->arvore_to_ul($raiz, "remove"));
		$this->registry['template']->show('common/banner');
		$this->registry['template']->show('categorias/remover');
		$this->registry['template']->show('common/footer');
    }
    
	function remove()
    {
    	$my_id = $this->registry['Uri']->getURIsegmento(2);
    	$my_linguas = $this->registry['lingua']->get_linguas_disponiveis();
    	$my_categoria = $this->registry['categorias']->get($my_id);
    	
    	if($_POST)
    	{
    		if($this->registry['categorias']->delete($my_id, $_POST))
    		{
    			header("location:/suser/index.php?route=categorias/remover");
				exit;
    		} else {
    			echo "<h1>ocorreu um erro</h1><br />";
    		}
    	} else {
    		$this->registry['template']->set('remove_id', $my_id);
    		$this->registry['template']->set('data', $my_categoria);
    		
    		$this->registry['template']->set('idiomas', $my_linguas);
			$this->registry['template']->show('common/banner');
			$this->registry['template']->show('categorias/remove');
			$this->registry['template']->show('common/footer');
    	}
    }
    
}
?>